package com.jarvan.controller;

import com.jarvan.dto.StationDto;
import com.jarvan.enums.ResultEnum;
import com.jarvan.enums.StationEnum;
import com.jarvan.exception.TrainException;
import com.jarvan.form.StationEditForm;
import com.jarvan.form.StationForm;
import com.jarvan.model.Station;
import com.jarvan.service.impl.StationServiceImpl;
import com.jarvan.utils.ResultVoUtil;
import com.jarvan.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "站点增删改查")
@Slf4j
@RequestMapping("/admin/station")
@RestController
public class AdminStationController {
    @Autowired
    private StationServiceImpl stationService;

    @ApiOperation(value = "Create")
    @PostMapping("/save")
    public ResultVo<Station> save(@Valid @RequestBody StationForm stationForm){
        log.info("[新增站点] -- stationForm={}", stationForm);
//        if (bindingResult.hasErrors()){
//            log.error("[新增站点] 参数不全  stationForm={},bindingResult={}", stationForm, bindingResult);
//            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(),
//                    Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
//        }
        Station station = new Station();
        BeanUtils.copyProperties(stationForm, station);
        Station result = stationService.save(station);
        return ResultVoUtil.success(result);
    }


    @GetMapping("/stationList")
    public ResultVo<StationDto> getStationList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(name = "size", defaultValue = "10") Integer size){
        Page<Station> pages = stationService.findAll(PageRequest.of(page - 1, size));
        StationDto dto = new StationDto();
        dto.setStationList(pages.getContent());
        dto.setCount(pages.getTotalElements());
        return ResultVoUtil.success(dto);
    }

    @GetMapping("/findByName")
    public ResultVo<List<Station>> getStationByName(@RequestParam("stationName") String stationName){

        if (stationName.isEmpty()){
            log.warn("[查询车站] 参数未传，默认所有");
            List<Station> result = stationService.findAll(PageRequest.of(0, 10)).getContent();
            return ResultVoUtil.success(result);
        }
        List<Station> stationList = new ArrayList<>();
        Station result = stationService.findbyStationName(stationName);
        stationList.add(result);
        return ResultVoUtil.success(stationList);
    }

    @GetMapping("/findByStatus")
    public ResultVo<List<Station>> getStationByStatus(@RequestParam("status") Integer status){
        if (status.toString().isEmpty()){
            log.error("[站点查询]  参数不全  status={}", status);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        List<Station> stations = stationService.findByStatus(status);
        return ResultVoUtil.success(stations);
    }

//    @GetMapping("/update")
//    public ResultVo<Station> changeStatus(@RequestParam("stationId") Integer stationId,
//                                          @RequestParam("status") Integer status){
//        if (stationId.toString().isEmpty() || status.toString().isEmpty()){
//            log.error("[修改状态]  参数不全 stationId={},status={}", stationId, status);
//            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
//        }
//        Station station = stationService.findById(stationId);
//        station.setStatus(status);
//        Station result = stationService.save(station);
//        return  ResultVoUtil.success(result);
//    }

    @PostMapping("/edit")
    public ResultVo<Station> stationEdit(@Valid @RequestBody StationEditForm stationEditForm){
        log.info("[修改状态] -- stationEditForm={}", stationEditForm);
        if (stationEditForm.getStationId().toString().isEmpty()){
            log.error("[站点查询]  参数不全  stationEditForm={}", stationEditForm);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Station station = new Station();
        BeanUtils.copyProperties(stationEditForm, station);
        Station result = stationService.save(station);
        return  ResultVoUtil.success(result);
    }

    @GetMapping("delete")
    public ResultVo<Station> delete(@RequestParam("stationId") Integer stationId){
        if (stationId.toString().isEmpty()){
            log.error("[站点查询]  参数不全  stationId={}", stationId);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Station station = stationService.findById(stationId);
        station.setStatus(StationEnum.OUTOFSERVICE.getCode());
        stationService.save(station);
        return ResultVoUtil.success();
    }

    @GetMapping("recover")
    public ResultVo<Station> recover(@RequestParam("stationId") Integer stationId){
        if (stationId.toString().isEmpty()){
            log.error("[站点查询]  参数不全  stationId={}", stationId);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Station station = stationService.findById(stationId);
        station.setStatus(StationEnum.NORMAL.getCode());
        stationService.save(station);
        return ResultVoUtil.success();
    }
}
